<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <!-- <el-form-item label="台账编号" prop="propertyAssetLedgerId">
            <el-input
              v-model="queryParams.propertyAssetLedgerId"
              placeholder="请输入台账编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item> -->
          <!-- <el-form-item label="项目编号" prop="projectId">
            <el-input v-model="queryParams.projectId" placeholder="请输入项目id" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item> -->
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="出库数量" prop="outboundQuantity">
            <el-input v-model="queryParams.outboundQuantity" placeholder="请输入出库数量" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="单价" prop="univalence">
            <el-input v-model="queryParams.univalence" placeholder="请输入单价" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="合计" prop="total">
            <el-input v-model="queryParams.total" placeholder="请输入合计" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="入库数量" prop="inventoryQuantity">
            <el-input
              v-model="queryParams.inventoryQuantity"
              placeholder="请输入入库数量"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="出库时间" prop="outboundTime">
            <el-date-picker clearable v-model="queryParams.outboundTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择出库时间" />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:assetOutboundRecords:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" @click="handleUpdate()" v-hasPermi="['business:assetOutboundRecords:edit']">修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['business:assetOutboundRecords:remove']"
              >删除</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:assetOutboundRecords:export']"
              >导出</el-button
            >
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="assetOutboundRecordsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="项目编号" align="center" prop="projectId" /> -->
        <el-table-column label="类别" align="center" prop="category">
          <template #default="scope">
            <span>{{getCategory(scope.row.category)}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="台账编号" align="center" prop="propertyAssetLedgerId" /> -->
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="型号" align="center" prop="model" />
        <el-table-column label="单位" align="center" prop="unit" />
        <!-- <el-table-column label="库存数量" align="center" prop="quantity" /> -->
        <el-table-column label="单价" align="center" prop="univalence" />
        <!-- <el-table-column label="合计" align="center" prop="total" /> -->
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="出库数量" align="center" prop="outboundQuantity" />
        <el-table-column label="出库时间" align="center" prop="outboundTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.outboundTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['business:assetOutboundRecords:edit']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['business:assetOutboundRecords:remove']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改资产出库记录对话框 -->
    <!-- <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="assetOutboundRecordsFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="台账id" prop="propertyAssetLedgerId">
          <el-input v-model="form.propertyAssetLedgerId" placeholder="请输入台账id" />
        </el-form-item>
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="出库数量" prop="outboundQuantity">
          <el-input v-model="form.outboundQuantity" placeholder="请输入出库数量" />
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input v-model="form.univalence" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="合计" prop="total">
          <el-input v-model="form.total" placeholder="请输入合计" />
        </el-form-item>
        <el-form-item label="入库数量" prop="inventoryQuantity">
          <el-input v-model="form.inventoryQuantity" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="出库时间" prop="outboundTime">
          <el-date-picker clearable v-model="form.outboundTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择出库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->
  </div>
</template>

<script setup lang="ts">
import { listAssetOutboundRecords, getAssetOutboundRecords, delAssetOutboundRecords, addAssetOutboundRecords, updateAssetOutboundRecords } from '@/api/business/assetOutboundRecords/index';
import { AssetOutboundRecordsVO, AssetOutboundRecordsQuery, AssetOutboundRecordsForm } from '@/api/business/assetOutboundRecords/types';
import { listAssetCategory } from '@/api/business/assetCategory/index';

const router = useRouter();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const assetOutboundRecordsList = ref<AssetOutboundRecordsVO[]>([]);
// const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
// const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const categoryData = ref([])

const queryFormRef = ref<ElFormInstance>();
// const assetOutboundRecordsFormRef = ref<ElFormInstance>();

// const dialog = reactive<DialogOption>({
//   visible: false,
//   title: ''
// });

const initFormData: AssetOutboundRecordsForm = {
  id: undefined,
  propertyAssetLedgerId: undefined,
  type:"2",
  projectId: undefined,
  projectName: undefined,
  outboundQuantity: undefined,
  univalence: undefined,
  total: undefined,
  inventoryQuantity: undefined,
  outboundTime: undefined,
  remark: undefined,
}
const data = reactive<PageData<AssetOutboundRecordsForm, AssetOutboundRecordsQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type:2,
    propertyAssetLedgerId: undefined,
    projectId: undefined,
    projectName: undefined,
    outboundQuantity: undefined,
    univalence: undefined,
    total: undefined,
    inventoryQuantity: undefined,
    outboundTime: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "资产出库记录id不能为空", trigger: "blur" }
    ],
    propertyAssetLedgerId: [
      { required: true, message: "台账id不能为空", trigger: "blur" }
    ],
    projectId: [
      { required: true, message: "项目id不能为空", trigger: "blur" }
    ],
    projectName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    outboundQuantity: [
      { required: true, message: "出库数量不能为空", trigger: "blur" }
    ],
    univalence: [
      { required: true, message: "单价不能为空", trigger: "blur" }
    ],
    total: [
      { required: true, message: "合计不能为空", trigger: "blur" }
    ],
    inventoryQuantity: [
      { required: true, message: "入库数量不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资产出库记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAssetOutboundRecords(queryParams.value);
  assetOutboundRecordsList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
// const cancel = () => {
//   reset();
//   dialog.visible = false;
// }

// /** 表单重置 */
// const reset = () => {
//   form.value = {...initFormData};
//   assetOutboundRecordsFormRef.value?.resetFields();
// }

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: AssetOutboundRecordsVO[]) => {
  ids.value = selection.map(item => item.id);
  // single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({ path: '/NotFixedAssetManagement/NotFixedAssetManagementOutbound/NotFixedAssetManagementOutboundAdd', query: { type:'add' } })
  // reset();
  // dialog.visible = true;
  // dialog.title = "添加资产出库记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: AssetOutboundRecordsVO) => {
  router.push({ path: '/NotFixedAssetManagement/NotFixedAssetManagementOutbound/NotFixedAssetManagementOutboundAdd', query: { type:'edit',id: row.id} })
  const _id = row?.id || ids.value[0]
  const res = await getAssetOutboundRecords(_id);
  Object.assign(form.value, res.data);
  // dialog.visible = true;
  // dialog.title = "修改资产出库记录";
}

// /** 提交按钮 */
// const submitForm = () => {
//   assetOutboundRecordsFormRef.value?.validate(async (valid: boolean) => {
//     if (valid) {
//       buttonLoading.value = true;
//       if (form.value.id) {
//         await updateAssetOutboundRecords(form.value).finally(() =>  buttonLoading.value = false);
//       } else {
//         await addAssetOutboundRecords(form.value).finally(() =>  buttonLoading.value = false);
//       }
//       proxy?.$modal.msgSuccess("修改成功");
//       dialog.visible = false;
//       await getList();
//     }
//   });
// }

/** 删除按钮操作 */
const handleDelete = async (row?: AssetOutboundRecordsVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除资产出库记录编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delAssetOutboundRecords(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

const getCategory = (data) =>{
  if (data && categoryData.value) {
    let obj = categoryData.value.find(item=>item.id ==data)
    return obj?.category
  }
}

const getCategoryData = async () =>{
  const res = await listAssetCategory();
  categoryData.value = res.rows
}


onMounted(() => {
  getCategoryData()
  getList();
});
</script>
